//
//  string.h
//  mystring_10_20
//
//  Created by s1nzz xie on 2023/10/20.
//

#ifndef string_h
#define string_h


#endif /* string_h */

#include<iostream>
#include<assert.h>
#include<string.h>
#include<algorithm>

using namespace std;

namespace xyx
{
    class string
{
public:
    typedef char* iterator;
    typedef const char* const_iterator;
    
    iterator begin()
    {
        return _str;
    }
    
    iterator end()
    {
        return _str + _size;
    }
    
    const_iterator begin() const
    {
        return _str;
    }
    
    const_iterator end() const
    {
        return _str + _size;
    }
    
    string(const char* str = "")
    {
        size_t len = strlen(str);
        _capacity = _size = len;
        _str = new char[_capacity+1];//多开辟一个空间放'\0'
        strcpy(_str,str);
    }
    ~string()
    {
        delete[] _str;
        _size = _capacity = 0;
    }
    
    const char* c_str() const
    {
        return _str;
    }
    
    size_t size() const
    {
        return _size;
    }
    
    size_t capacity() const
    {
        return _capacity;
    }
    
    char& operator[](size_t pos)
    {
        assert(pos <= _size);
        return _str[pos];
    }
    
    const char& operator[](size_t pos) const
    {
        assert(pos <= _size);
        return _str[pos];
    }
    
    void reserve(size_t n)
    {
        if(n > _capacity)
        {
            char* tmp = new char[n];
            memcpy(tmp,_str,_size + 1);
            
            delete[] _str;
            _capacity = n;
            _str = tmp;
            
        }
    }
    
    void resize(size_t n, char ch = '\0')
    {
        if(n < _size)
        {
            _size = n;
            _str[n] = '\0';
        }
        else
        {
            reserve(n);
            for(size_t i = _size; i < n; ++i)
            {
                _str[i] = ch;
            }
            _size = n;
        }
    }
    
    void push_back(char ch)
    {
        if(_capacity == _size)
        {
            reserve(_capacity == 0 ? 4 : 2 * _capacity);
        }
        
        _str[_size++] = ch;
        _str[_size] = '\0';
    }
    
    void append(const char* str)
    {
        //判断容量
        size_t len = strlen(str);
        
        if(len + _size > _capacity)
        {
            reserve((len + _capacity) * 2);
        }
        
        //拷贝数据
        memcpy(_str+_size, str, len + 1);
        
        _size += len;
    }
    
    string& operator += (const char* str)
    {
        append(str);
        return *this;
    }
    
    string& operator += (char ch)
    {
        push_back(ch);
        return *this;
    }
    
    void insert(size_t pos,size_t n, char ch)
    {
        //判断下标
        assert(pos <= _size);
        //判断容量
        if(_size + n > _capacity)
        {
            reserve((n + _capacity) * 2);
        }
        //挪动数据
        size_t end = _size;
        //由于是定义的size_t,当end为0时，--end就会出现问题。
        while(end >= pos && end != npos )
        {
            _str[end + n] = _str[end];
            end--;
        }
        
        //插入n个字符
        for(size_t i = 0; i < n; i++)
        {
            _str[i+pos] = ch;
        }
        _size += n;
    }
    
    void insert(size_t pos, const char* str)
    {
        //判断下标
        assert(pos <= _size);
        
        //判断容量
        size_t n = strlen(str);
        if(_size + n > _capacity)
        {
            reserve((n + _capacity) * 2);
        }
        
        //向后挪动数据
        size_t end = _size;
        while(end >= pos && end != npos)
        {
            _str[end + n] = _str[end];
            end--;
        }
        
        //插入数据
        for(size_t i = 0; i < n; i++)
        {
            _str[i+pos] = str[i];
        }
        _size += n;
        
    }
    size_t find(const char* str, size_t pos = 0) const
    {
        assert(pos < _size);
        
        const char* tmp = strstr(_str + pos,str);
        if(tmp)
            return tmp-_str;
        else
            return npos;
    }
    
    
    size_t find(char ch, size_t pos = 0) const
    {
        assert(pos < _size);
        
        for(size_t i = pos; i < _size; i++)
        {
            if(_str[i] == ch)
                return i;
        }
        
        return npos;
    }
    
    void erase(size_t pos, size_t len = npos)
    {
        assert(pos < _size);
        
        //从pos位置开始全删除
        if(len == npos || pos + len <= _size)
        {
            _size = pos;
            _str[_size] = '\0';
        }
        else
        {
            size_t end = pos + len;
            while(end <= _size)
            {
                _str[pos++] = _str[end++];
            }
            
            _size -= len;
        }
    }
    
    bool operator<(const string& s) const
    {
        //先用_str和s._str两个中长度较短的进行比较
        int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
        
        //"hello" "hello"    false
        //"hello" "hell"     false
        //"hello" "helloxxx" ture
        
        //ret为0，若_size小则返回真，否则返回假
        //若ret不小于0直接为假
        return ret == 0 ? _size < s._size : ret < 0;
    }
    
    bool operator==(const string& s) const
    {
        if(s._size != _size)
            return false;
        else
            return memcmp(_str, s._str,_size) == 0;
    }
    
    bool operator<=(const string& s) const
    {
        return *this < s || *this == s;
    }
    
    bool operator>(const string& s) const
    {
        return !(*this <= s);
    }
    
    bool operator>=(const string& s) const
    {
        return !(*this < s);
    }
    
    bool operator!=(const string& s) const
    {
        return !(*this == s);
    }
    
    //        string(const string& s)
    //        {
    //            _str = new char[s._capacity+1];
    //            memcpy(_str,s._str, s._size + 1);
    //            _size = s._size;
    //            _capacity = s._capacity;
    //        }
    
    void swap(string& s)
    {
        std::swap(_str,s._str);
        std::swap(_size,s._size);
        std::swap(_capacity,s._capacity);
    }
    
    string(const string& s)
        :_str(nullptr)
        ,_size(0)
        ,_capacity(0)
    {
        string tmp(s._str);
        swap(tmp);
    }
        
    string& operator=(string tmp)
    {
        swap(tmp);
        return *this;
    }
    
    string substr(size_t pos = 0, size_t len = npos)const
    {
        assert(pos < _size);
        
        string tmp;
        size_t n = len;
        
        //判断长度
        if(len == npos || pos + len > _size)
            n = _size - pos;
        
        tmp.reserve(n);
            
        for(size_t i = pos; i < pos + n; i++)
        {
            tmp += _str[i];
        }
        
        return tmp;
    }
    
    void clear()
    {
        _str[0] = '\0';
        _size = 0;
    }
        
    private:
        char* _str;//指向堆区开辟的空间
        size_t _size;//记录有效数据个数
        size_t _capacity;//记录堆区空间的大小
        
    public:
        const static size_t npos;
    };

    const size_t string::npos = -1;
    
    ostream& operator<<(ostream& out, const string& str)
    {
        for(auto e: str)
        {
            out << e;
        }
        
        return out;
    }

    istream& operator>>(istream& in, string& str)
    {
        str.clear();
        
        char ch = in.get();
        //避免扩容
        char buffer[128] = {'\0'};
        int i = 0;
        
        //清除缓冲区的换行或者空格
        while(ch == ' ' || ch == '\n')
            ch = in.get();
        
        while(ch != '\n')
        {
            buffer[i++] = ch;
            if(i == 127)
            {
                buffer[i] = '\0';
                str += buffer;
                i = 0;
            }
            //读数据
            ch = in.get();
        }
        
        //i不为0表示还有数据
        if(i)
        {
            //在i下标处添加\0，避免+=数据错误
            buffer[i] = '\0';
            str += buffer;
        }
        
        return in;
    }
 
}

